package com.itheima.service;

import com.itheima.domain.Student;
import com.itheima.mapper.StudentMapper;
import com.itheima.utils.MapperUtils;

import java.util.List;

public class StudentServiceImpl implements StudentService {
    @Override
    public List<Student> findAll() {
        try{
            StudentMapper studentMapper = MapperUtils.getMapper(StudentMapper.class);
            List<Student> students = studentMapper.findAll();
            return students;
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            MapperUtils.close();
        }
        return null;
    }
    @Override
    public List<Student> findByNameAndAge(String name, int age) {
        try{
            StudentMapper studentMapper = MapperUtils.getMapper(StudentMapper.class);
            List<Student> students = studentMapper.findByNameAndAge(name, age);
            MapperUtils.commit();
            return students;
        }catch(Exception e){
            MapperUtils.rollback();
            e.printStackTrace();
        }finally{
            MapperUtils.close();
        }
        return null;
    }

    @Override
    public Integer addStu(Student stu) {
        try{
            StudentMapper studentMapper = MapperUtils.getMapper(StudentMapper.class);
            Integer result = studentMapper.addStu(stu);
            MapperUtils.commit();
            return result;
        }catch(Exception e){
            e.printStackTrace();
            MapperUtils.rollback();
        }finally{
            MapperUtils.close();
        }

        return -1;
    }

    @Override
    public Integer updateStu(Student stu) {
        try{
            StudentMapper studentMapper = MapperUtils.getMapper(StudentMapper.class);
            Integer result = studentMapper.updateStu(stu);
            MapperUtils.commit();
            return result;
        }catch(Exception e){
            e.printStackTrace();
            MapperUtils.rollback();
        }finally{
            MapperUtils.close();
        }

        return -1;
    }

    @Override
    public Integer deleteStu(Integer id) {
        try{
            StudentMapper studentMapper = MapperUtils.getMapper(StudentMapper.class);
            Integer result = studentMapper.deleteStu(id);
            MapperUtils.commit();
            return result;
        }catch(Exception e){
            e.printStackTrace();
            MapperUtils.rollback();
        }finally{
            MapperUtils.close();
        }

        return -1;
    }


}
